package com.example.hyg_veintype.activity.account.userCenter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.example.hyg_veintype.BuildConfig;
import com.example.hyg_veintype.R;
import com.example.hyg_veintype.activity.account.login.LoginActivity;
import com.example.hyg_veintype.base.BaseVeinTypeActivity;
import com.example.hyg_veintype.base.api.ApiServiceManager;
import com.example.hyg_veintype.base.api.IGsonResponse;
import com.example.hyg_veintype.constant.account.AccountConst;
import com.example.hyg_veintype.data.net.logout.LogoutWraper;
import com.example.hyg_veintype.utils.AppManager;
import com.hyg.lib_common.sweetalertdialog.SweetAlertDialog;


import okhttp3.FormBody;

import static com.hyg.lib_common.constant.UrlUtils.logoutAddr;

/**
 * @author: yyw
 * time: 2020/12/31 10:40
 * desc: 用户中心页面
 */
public class UserCenterActivity extends BaseVeinTypeActivity{

    RelativeLayout rlLogout;

    SweetAlertDialog pDialog;

    public static void start(Context context) {
        Intent starter = new Intent(context, UserCenterActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.veintype_activity_user_center;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tvTitle.setText("用户中心");
        rlLogout=findViewById(R.id.rl_logout);
        rlLogout.setOnClickListener(this);
        pDialog = new SweetAlertDialog(UserCenterActivity.this);
        if(BuildConfig.app_vein_type){
            rlLogout.setVisibility(View.VISIBLE);
        }else{
            rlLogout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.rl_logout) {
            showLogoutDialog();
        }
    }

    private void showLogoutDialog() {
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }
        pDialog = new SweetAlertDialog(UserCenterActivity.this, SweetAlertDialog.NORMAL_TYPE);
        pDialog.setTitleText("确定注销当前账号？")
                .setConfirmButton("确定", new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                        doLogout();
                    }
                })
                .setCancelButton("取消", new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        sweetAlertDialog.dismiss();
                    }
                })
                .show();
    }

    private void doLogout(){
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }
        pDialog = new SweetAlertDialog(UserCenterActivity.this, SweetAlertDialog.ERROR_TYPE);
        pDialog.setConfirmButton("好，知道啦", new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
                sweetAlertDialog.dismiss();
            }
        });
        FormBody formBody = new FormBody.Builder()
                .add("phone" , AccountConst.phone)
                .add("token", AccountConst.token)
                .build();
        ApiServiceManager.getInstance().postRequest(logoutAddr, formBody, LogoutWraper.class, new IGsonResponse<LogoutWraper>() {
            @Override
            public void onSuccess(LogoutWraper model, String response) {
                if(model.getCode()==200){
                    AccountConst.saveAccountData(UserCenterActivity.this,"","","");
                    AppManager.getAppManager().finishAllActivity();
                    LoginActivity.start(UserCenterActivity.this);
                }else{
                    pDialog.setTitleText("退出登录失败").setContentText(model.getMessage()).show();
                }
            }

            @Override
            public void onError(String msg) {
                pDialog.setTitleText("退出登录失败").setContentText("网络异常").show();
            }
        });
    }

}
